package com.ts.code.creator;

import com.ts.code.creator.util.CreateCodeContext;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiJavaFile;

import static com.ts.code.creator.util.StringUtil.getEntityNameFromControllerName;


public class CreateCodeAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent event) {
        Project project = event.getProject();

        PsiJavaFile file = (PsiJavaFile) event.getData(CommonDataKeys.PSI_FILE);
//
        PsiClass[] classes = file.getClasses();
        PsiClass psiClass = classes[0];
        // 实体类名称
        String entityName = getEntityNameFromControllerName(psiClass.getName());

        String packageName = file.getPackageName();
        String className = psiClass.getName();

        CreateCodeContext context = new CreateCodeContext();
        context.setProject(project);
        context.setAdminControllerFile(file);
        context.setAdminControllerPackageName(packageName);
        context.setAdminControllerClassName(className);
        context.setEntityName(entityName);

        CreateCodeGui createCodeGui = new CreateCodeGui(project, true, context);
        createCodeGui.show();
    }

}
